package com.example.assemblyutil

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.downutil.Utils.Utils
import com.example.downutil.callback.DownloadCallback
import com.example.downutil.data.DownloadData
import com.example.downutil.download.DownloadManger
import kotlinx.android.synthetic.main.down_load_layout.*
import kotlinx.android.synthetic.main.title_layout.*
import java.io.File
import java.util.ArrayList

/**
 * descrition:
 *
 */
class DownLoadActivity: AppCompatActivity(){
    //下载路径 18108135245
    private val path = Environment.getExternalStorageDirectory().toString() + "/AssmeblyDown/"

    private val url1 =
        "https://ip203500912.out.azhimalayanvh.com/fs08/2022/06/13/11/106_4a641de3bb50143a09017f723df08421.apk?yingid=wdj_web&fname=%E5%BF%AB%E6%89%8B&productid=2011&pos=wdj_web%2Fdetail_normal_dl%2F0&appid=280621&packageid=601130478&apprd=280621&iconUrl=http%3A%2F%2Fandroid-artworks.25pp.com%2Ffs08%2F2022%2F06%2F10%2F6%2F110_1843953ec61b5c7cd791a3c6aceff451_con.png&pkg=com.smile.gifmaker&did=a4f42a3a86ceb32dee1a00bb8c14e27f&vcode=26042&md5=07a4aaf2735d64d5d9122858587ab735&ali_redirect_domain=alissl.ucdl.pp.uc.cn&ali_redirect_ex_ftag=f8b1716cbc9ee47abc0d8bb217afce2f69a617ed38757942&ali_redirect_ex_tmining_ts=1655108961&ali_redirect_ex_tmining_expire=3600&ali_redirect_ex_hot=100" //欢乐斗地主

    private val url2 =
        "https://ip203500912.out.azhimalayanvh.com/fs08/2022/06/09/8/120_00a40cf1a3ad2bb2eb6b57f371fe8dfe.apk?fname=%E6%8A%96%E9%9F%B3&productid=2011&packageid=500753338&pkg=com.ss.android.ugc.aweme&vcode=210001&yingid=wdj_web&pos=wdj_web%2Fdetail_normal_dl%2F0&appid=7461948&apprd=7461948&iconUrl=http%3A%2F%2Fandroid-artworks.25pp.com%2Ffs08%2F2022%2F06%2F08%2F7%2F106_55f4a15f8f1b26704deb1cb68550f558_con.png&did=8bb789e8c03d0f4cf5e7897165a5e6ee&md5=097ad49b43de28fdfd925503e8cdb243&ali_redirect_domain=alissl.ucdl.pp.uc.cn&ali_redirect_ex_ftag=b05ab0b4d9313d5f603d136843bcbbbf82152d33e2a399e2&ali_redirect_ex_tmining_ts=1655109006&ali_redirect_ex_tmining_expire=3600&ali_redirect_ex_hot=100" //欢乐斗地主

    private val url3 =
        "https://ip3791688484.mobgslb.tbcache.com/fs08/2022/06/07/0/110_c63096dbc703011da40728be522166aa.apk?yingid=wdj_web&fname=%E9%85%B7%E7%8B%97%E9%9F%B3%E4%B9%90&productid=2011&pos=wdj_web%2Fdetail_normal_dl%2F0&appid=34221&packageid=601129536&apprd=34221&iconUrl=http%3A%2F%2Fandroid-artworks.25pp.com%2Ffs08%2F2022%2F06%2F09%2F6%2F110_3404693f314e184d2c01c2bf532feac3_con.png&pkg=com.kugou.android&did=c7b35baa55438bae04da0dba17ac8cfb&vcode=11209&md5=8850a0246b99cbd4b4d1442452b044d8&ali_redirect_domain=alissl.ucdl.pp.uc.cn&ali_redirect_ex_ftag=15f7c4e02f733d654c2d22a19f271b0b38b4ed411461afbf&ali_redirect_ex_tmining_ts=1655109045&ali_redirect_ex_tmining_expire=3600&ali_redirect_ex_hot=100" //欢乐斗地主

    private val url4 =
        "https://ip3498488181.out.azhimalayanvh.com/fs08/2022/06/02/9/120_9d61c6be4327fa0d321be3226584eabf.apk?fname=QQ%E9%9F%B3%E4%B9%90&productid=2011&packageid=401110057&pkg=com.tencent.qqmusic&vcode=3078&yingid=wdj_web&pos=wdj_web%2Fdetail_normal_dl%2F0&appid=280003&apprd=280003&iconUrl=http%3A%2F%2Fandroid-artworks.25pp.com%2Ffs08%2F2022%2F05%2F11%2F0%2F123_5a3f8ebf42709e19ba3c9a2263cb3263_con.png&did=090eca864bdf7d8418321682aa45cbf7&md5=d6f6d42cc234d16547571c771e6019d0&ali_redirect_domain=alissl.ucdl.pp.uc.cn&ali_redirect_ex_ftag=2dfd6dfcc1e273f7aa0e83d9bb8ed7d13612db0a6086f2d3&ali_redirect_ex_tmining_ts=1655109073&ali_redirect_ex_tmining_expire=3600&ali_redirect_ex_hot=100" //欢乐斗地主

    private val url5 =
        "https://ip220278128.out.azhimalayanvh.com/fs08/2022/06/02/2/123_3558937149a497575bbbc75fd63e9ab4.apk?yingid=wdj_web&fname=%E8%85%BE%E8%AE%AF%E8%A7%86%E9%A2%91&productid=2011&pos=wdj_web%2Fdetail_normal_dl%2F0&appid=280237&packageid=601128745&apprd=280237&iconUrl=http%3A%2F%2Fandroid-artworks.25pp.com%2Ffs08%2F2022%2F05%2F11%2F4%2F123_90a9374cbf70cab427727b6979f67f5b_con.png&pkg=com.tencent.qqlive&did=08a9b78b8d43b9e56057323686b858f8&vcode=26745&md5=1de505600a7a6c2afa77f6e1bc53ba2b&ali_redirect_domain=alissl.ucdl.pp.uc.cn&ali_redirect_ex_ftag=01d8fdcdde37030eea9f92363612ed0201a6d3a195594c7e&ali_redirect_ex_tmining_ts=1655109095&ali_redirect_ex_tmining_expire=3600&ali_redirect_ex_hot=100" //欢乐斗地主


    val mList:MutableList<DownloadData> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.down_load_layout)
        title_text.text = "批量下载"

        initData()
        initListener()
    }

   private fun initData(){
        mList.add(DownloadData(url1,path,"糖果大作战"))
        mList.add(DownloadData(url2,path,"捕鱼达人"))
        mList.add(DownloadData(url3,path,"开心消消乐"))
        mList.add(DownloadData(url4,path,"王者荣耀"))
        mList.add(DownloadData(url5,path,"爱奇艺"))

        down_list.adapter = FollowListAdapter()
    }

    @SuppressLint("NewApi", "UseCompatLoadingForDrawables")
    private fun initListener(){
        back_image.setOnClickListener {
            finish()
        }
        start_down.setOnClickListener {
            start_down.background = resources.getDrawable(R.drawable.bg_a_style_04,null)

            mList.forEach {
                DownloadManger.getInstance(this@DownLoadActivity).start(it.url)
            }
        }
    }


    /**
     * adapter
     */
    inner class FollowListAdapter : RecyclerView.Adapter<FollowListAdapter.OneViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OneViewHolder {
            return OneViewHolder(
                layoutInflater.inflate(
                    R.layout.down_list_item,
                    parent,
                    false
                )
            )
        }

        @SuppressLint("NewApi")
        override fun onBindViewHolder(holder: OneViewHolder, position: Int) {
            holder.name.text = mList[position].name
            //开始
            holder.start.setOnClickListener {
                DownloadManger.getInstance(this@DownLoadActivity).start(mList[position].url)
            }
            //暂停
            holder.pause.setOnClickListener {
                DownloadManger.getInstance(this@DownLoadActivity).pause(mList[position].url)
            }
            //继续
            holder.resume.setOnClickListener {
                DownloadManger.getInstance(this@DownLoadActivity).resume(mList[position].url)
            }
            //取消
            holder.cancel.setOnClickListener {
                DownloadManger.getInstance(this@DownLoadActivity).cancel(mList[position].url)
            }

            setListener(holder, mList[position])

        }

        override fun getItemCount(): Int {
            return mList.size
        }
        inner class OneViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
           val progressBar: ProgressBar = itemView.findViewById(R.id.progress_bar)
            val downloadSize: TextView = itemView.findViewById(R.id.download_size)
            val percentage: TextView = itemView.findViewById(R.id.percentage)
            val resume: TextView = itemView.findViewById(R.id.resume)
            val cancel: TextView = itemView.findViewById(R.id.cancel)

            val start: TextView = itemView.findViewById(R.id.start)
            val pause: TextView = itemView.findViewById(R.id.pause)
            val name: TextView = itemView.findViewById(R.id.name)

        }
    }

    @SuppressLint("SetTextI18n")
    private fun setListener(viewHolder: FollowListAdapter.OneViewHolder, downloadData: DownloadData) {
        DownloadManger.getInstance(this)
            .setOnDownloadCallback(downloadData, object : DownloadCallback {
                override fun onStart(currentSize: Long, totalSize: Long, progress: Float) {
                    Log.e("下载监听",downloadData.name.toString() + ":准备中")
                }

                override fun onProgress(currentSize: Long, totalSize: Long, progress: Float) {
                    Log.e("下载监听",downloadData.name.toString() + ":下载中")
                    viewHolder.progressBar.progress = progress.toInt()
                    viewHolder.name.text = "${downloadData.name}:下载中"
                    viewHolder.downloadSize.text = Utils.formatSize(currentSize).toString() + "/" + Utils.formatSize(totalSize)
                    viewHolder.percentage.text = "$progress%"
                }


                override fun onPause() {
                    Log.e("下载监听",downloadData.name.toString() + ":已暂停")
                    viewHolder.name.text = "${downloadData.name}:已暂停"

                }

                override fun onCancel() {
                    Log.e("下载监听",downloadData.name.toString() + ":已取消")
                    viewHolder.name.text = "${downloadData.name}:已取消"

                }

                override fun onFinish(file: File?) {
                    Log.e("下载监听",downloadData.name.toString() + ":已完成")
                    viewHolder.name.text = "${downloadData.name}:已完成"

                }

                override fun onWait() {
                    Log.e("下载监听",downloadData.name.toString() + ":等待中")
                    viewHolder.name.text = "${downloadData.name}:等待中"

                }

                override fun onError(error: String?) {
                    Log.e("下载监听",downloadData.name.toString() + ":出错:"+error)
                    viewHolder.name.text = "${downloadData.name}:出错 $error"

                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        DownloadManger.getInstance(this).destroy(mList as ArrayList<DownloadData>)
    }

}